<template>
  <el-dialog
    title="手机号验证"
    v-if="dialogVisible"
    :visible.sync="dialogVisible"
    :before-close="beforeDialogClose"
    append-to-body
    :close-on-click-modal="closeOnClickModal"
    width="500px"
  >
    <div class="login-box">
      <div style="color: #fff">
        {{ form.phone }}账号已存在，请输入该账号的验证码!
      </div>
      <div class="login-msg">
        <div class="login-type">
          <el-form :model="loginData" :rules="loginTypeRules" ref="loginData">
            <el-form-item prop="mobile">
              <div class="phone">
                <el-input
                  placeholder="请输入手机号码"
                  prefix-icon="iconfont iconmobile"
                  v-model.number="loginData.mobile"
                  maxlength="11"
                  disabled
                ></el-input>
              </div>
            </el-form-item>
            <el-form-item prop="code">
              <div class="code">
                <el-input
                  placeholder="请输入验证码"
                  prefix-icon="iconfont iconbianzu10"
                  v-model.trim="loginData.code"
                  maxlength="6"
                ></el-input>
                <el-button :disabled="disabled" @click="sendCode">{{
                  sendCodeText
                }}</el-button>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="beforeDialogClose">取消</el-button>
      <el-button type="primary" @click="submit" :loading="submitLoading"
        >确认</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { mapGetters } from "vuex";

export default {
  name: "validatePhone",
  props: {
    form: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    const validatePhone = (rule, value, callback) => {
      if (/^1[3456789]\d{9}$/.test(value)) {
        if (!this.sending) this.disabled = false;
        callback();
      } else {
        this.disabled = true;
        callback(new Error("请输入正确的手机号码"));
      }
    };

    const validateCode = (rule, value, callback) => {
      if (/^[0-9]{6}$/.test(value)) {
        this.suffixIcon = true;
        callback();
      } else {
        callback(new Error("请输入正确的验证码"));
      }
    };
    return {
      closeOnClickModal: false,
      submitLoading: false,
      dialogVisible: false,
      loginData: {
        mobile: "",
        code: "",
      },
      loginTypeRules: {
        mobile: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          { validator: validatePhone, trigger: "change" },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          { validator: validateCode, trigger: "blur" },
        ],
      },
      timeCount: 120,
      sendCodeText: "获取验证码",
      timer: "",
      sending: false,
      disabled: false,
      suffixIcon: false,
    };
  },
  computed: {
    ...mapGetters(["getUserInfo", "getUserID"]),
  },
  methods: {
    async init() {
      this.dialogVisible = true;
      this.loginData.mobile = this.form.phone;
    },
    sendCode() {
      this.disabled = true;

      let { mobile } = this.loginData;
      //   const formData = new FormData();
      //   formData.append("mobile", mobile);
      let params = {
        createdBy: this.$userInfo.phone,
        orgId: this.$userInfo.orgId,
        orgName: this.$userInfo.orgName,
        mobile,
      };
      this.$api.AdminController.code({ body: params })
        .then((res) => {
          if (res && res.code === 0) {
            console.log("res", res);
            this.timer = setInterval(() => {
              this.timeCount--;
              this.sendCodeText = `${this.timeCount}s后可重发`;
              this.disabled = true;
              this.sending = true;
              if (this.timeCount <= 0) {
                this.timeCount = 120;
                this.sendCodeText = `重发验证码`;
                this.disabled = false;
                this.sending = false;
                clearInterval(this.timer);
              }
            }, 1000);
          } else {
            //   notification.error({
            //     message: res.msg
            //   });
            this.disabled = false;
            this.loading = false;
          }
        })
        .catch((err) => {
          this.loading = false;
          this.disabled = false;
          clearInterval(this.timer);
        })
        .finally(() => {
          this.disabled = false;
          this.loading = false;
        });
    },
    submit() {
      this.$refs.loginData.validate((valid) => {
        if (valid) {
          this.submitLoading = true;
          this.$api.AdminController.check({ body: this.loginData })
            .then((res) => {
              if (res && res.code === 0) {
                this.$emit("success");
                this.beforeDialogClose();
                this.submitLoading = false;
              } else {
                this.submitLoading = false;
              }
            })
            .catch(() => {
              this.submitLoading = false;
            });
        }
      });
    },
    // 弹窗关闭前执行
    beforeDialogClose() {
      this.$refs.loginData.resetFields();
      this.dialogVisible = false;
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep {
  .el-form-item__content {
    margin-left: 0px;
    display: flex;
    align-items: center;
  }
}
.login-box {
  width: 400px;
  // height: 180px;
  margin: 0px auto 0;
  overflow: hidden;

  .login-msg {
    margin-top: 20px;
    .login-type {
      .phone {
        margin-bottom: 6px;
        width: 100%;
      }

      ::v-deep .el-input__prefix {
        width: 21px;
        font-size: 14px;
      }
      .code {
        overflow: hidden;
        .el-input {
          width: 290px;
          float: left;

          ::v-deep .el-icon-success {
            color: #52c41a;
          }
        }
        .el-input__icon {
          line-height: 40px;
        }
        button {
          float: right;
          font-size: 14px;
          color: rgba(0, 0, 0, 0.65);
          height: 40px;
          width: 102px;
          text-align: center;
          line-height: 40px;
          border: 1px solid rgba(217, 217, 217, 1);
          background-color: #fff;
          // border-radius: 4px;
          padding: 0;
          margin-left: 8px;
        }
      }
    }
  }
}
</style>
